package com.ycfmedical.bed.base;

import com.bilibili.magicasakura.utils.ThemeUtils;
import com.bilibili.magicasakura.widgets.TintToolbar;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.listener.CardPickerDialog;
import com.ycfmedical.bed.listener.RefreshCallBack;
import com.ycfmedical.bed.utils.RefreshDataUtils;
import com.ycfmedical.bed.utils.ThemeHelper;

import android.app.Activity;
import android.app.ActivityManager;
import android.os.Build;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.XActivity;

import  com.ycfmedical.bed.Constants;

/**
 * 带有切换主题meun和toolbar的 Activity 注意继承此Activity 必须 用通用的toolbar <include layout="@layout/view_toolbar" /> Created by Egoist
 * on 2017/1/19.
 */
public abstract class BaseMenuActivity<P extends IPresent> extends XActivity<P>
    implements CardPickerDialog.ClickListener,RefreshCallBack
{
    
    private static final String TAG = BaseMenuActivity.class.getSimpleName();
    
    private TextView mToolbarTitle,bed_name,bed_id,bed_doctor,bed_nurse,bed_heart,bed_temperature,bed_breathe;
    
    private ImageView mToolbarshare;
    
    private TintToolbar mToolbar;

    private RefreshDataUtils refreshDataUtils;

    @Override
    public void bindUI(View rootView)
    {
        super.bindUI(rootView);
        initToolbar();
        _onUiStart();
    }

    @Override
    public void setData(int flag,String data) {
        switch (flag){
            case Constants.BED_HEART:
                bed_heart.setText(data);
                break;
            case Constants.BED_TEMPERATURE:
                bed_temperature.setText(data);
                break;
            case Constants.BED_BREATHE:
                bed_breathe.setText(data);
                break;
            default:break;

        }

    }

    protected void initToolbar()
    {
        mToolbar = (TintToolbar)findViewById(R.id.toolbar);
        mToolbarTitle = (TextView)findViewById(R.id.toolbar_title);
        mToolbarshare = (ImageView)findViewById(R.id.toolbar_share);
        if (mToolbar != null)
        {
            setSupportActionBar(mToolbar); // 将Toolbar显示到界面
        }
        if (mToolbarTitle != null)
        {
            //getTitle()的值是activity的android:lable属性值
            mToolbarTitle.setText(getTitle());
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
    }

    private void _onUiStart()
    {
        /**
         * 判断是否有Toolbar,并默认显示返回按钮
         */
        if (null != getToolbar() && isShowBacking())
        {
            showBack();
        }
        else if (null != getToolbar() && isShowShare())
        {
            showShare();
        }
        else if (null != getToolbar() && isShowMenu())
        {
            showMenu();
        }
    }

    /**
     * 获取头部标题的TextView
     * 
     * @return
     */
    public TextView getToolbarTitle()
    {
        return mToolbarTitle;
    }
    
    /**
     * 获取头部标题的ShareView
     * 
     * @return
     */
    public ImageView getToolbarShare()
    {
        return mToolbarshare;
    }
    
    /**
     * 设置头部标题
     * 
     * @param title
     */
    public void setToolBarTitle(CharSequence title)
    {
        if (mToolbarTitle != null)
        {
            mToolbarTitle.setText(title);
        }
        else
        {
            getToolbar().setTitle(title);
            setSupportActionBar(getToolbar());
        }
    }
    
    /**
     * this Activity of tool bar. 获取头部.
     * 
     * @return support.v7.widget.Toolbar.
     */
    public TintToolbar getToolbar()
    {
        return (TintToolbar)findViewById(R.id.toolbar);
    }
    
    /**
     * 版本号小于21的后退按钮图片
     */
    private void showBack()
    {
        // setNavigationIcon必须在setSupportActionBar(toolbar);方法后面加入
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            if (getToolbar() != null)
            {
                setSupportActionBar(getToolbar());
                getSupportActionBar().setHomeButtonEnabled(true);
                getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_white_24dp);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }
        else
        {
            getToolbar().setNavigationIcon(R.drawable.ic_arrow_white_24dp);
            getToolbar().setNavigationOnClickListener((View v) -> {
                onBackPressed();
            });
        }
        
    }
    
    private void showShare()
    {
        if (mToolbarshare != null)
        {
            mToolbarshare.setVisibility(View.VISIBLE);
        }
    }
    
    private void showMenu()
    {
        // setNavigationIcon必须在setSupportActionBar(toolbar);方法后面加入
        getToolbar().setNavigationIcon(R.drawable.ic_arrow_white_24dp);
        getToolbar().setNavigationOnClickListener((View v) -> {
            onBackPressed();
        });
    }
    
    /**
     * 是否显示后退按钮,默认显示,可在子类重写该方法.
     * 
     * @return
     */
    protected boolean isShowBacking()
    {
        return true;
    }
    
    protected boolean isShowShare()
    {
        return false;
    }
    
    protected boolean isShowMenu()
    {
        return true;
    }
    
    // 设置弹出菜单
    @Override
    public int getOptionsMenuId()
    {
        return R.menu.menu_main;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        if (getOptionsMenuId() > 0)
        {
            getMenuInflater().inflate(getOptionsMenuId(), menu);
            return true;
        }
        else
        {
            return super.onCreateOptionsMenu(menu);
        }
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.menu_change_theme:
                
                CardPickerDialog dialog = new CardPickerDialog();
                dialog.setClickListener(this);
                dialog.show(getSupportFragmentManager(), CardPickerDialog.TAG);
                return true;
            case R.id.menu_bed_control:
                // ControlActivity.launch(this);
                return true;
            case R.id.menu_user_info:
                // UserinfoActivity.launch(this);
                return true;
            case android.R.id.home:
                finish();
                return true;
            case R.id.toolbar_share:
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    
    // 更换主题颜色接口
    @Override
    public void onConfirm(int currentTheme)
    {
        if (ThemeHelper.getTheme(BaseMenuActivity.this) != currentTheme)
        {
            ThemeHelper.setTheme(BaseMenuActivity.this, currentTheme);
            ThemeUtils.refreshUI(BaseMenuActivity.this, new ThemeUtils.ExtraRefreshable()
            {
                @Override
                public void refreshGlobal(Activity activity)
                {
                    // for global setting, just do once
                    if (Build.VERSION.SDK_INT >= 21)
                    {
                        final BaseMenuActivity context = BaseMenuActivity.this;
                        ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(null,
                            null, ThemeUtils.getThemeAttrColor(context, android.R.attr.colorPrimary));
                        setTaskDescription(taskDescription);
                        getWindow()
                            .setStatusBarColor(ThemeUtils.getColorById(context, R.color.theme_color_primary_dark));
                    }
                }
                
                @Override
                public void refreshSpecificView(View view)
                {
                }
            });
            
        }
        
    }
}
